package cn.itcast.foodapp2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.itcast.foodapp2.net.NetUtil;

public class CommentsAddActivity extends AppCompatActivity {
    EditText mEtComments;
    Button mBtnAdd;

    private static final String TAG = "CommentsAddActivity";
    private int userId;
    private int foodId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comments_add);
        initView();
        initData();
        initEvent();
    }

    private void initData() {
        //初始化数据，从Intent
//        userId = getIntent().getIntExtra("userId", -1);
        SharedPreferences sp = getSharedPreferences("data.sp", MODE_PRIVATE);
        userId = sp.getInt("userId", -1);

        foodId = getIntent().getIntExtra("foodId", -1);
    }

    private void initEvent() {
        mBtnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO  访问网络，提交评论
                // 准备url,jsonObject
                final String comment = mEtComments.getText().toString();//TODO 获取用户输入的评论信息
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        /*TODO  postDate 通过new Data()获取当前的系统时间，然后利用SimpleDateFormat转换为目标格式字符串*/

                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String postDate = sdf.format(date);
//                        Log.e(TAG, "run: " + postDate);
//                        // userId
//                        Log.e(TAG, "run: " + userId);
//                        // foodId
//                        Log.e(TAG, "run: " + foodId);

                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("comment", comment);
                            jsonObject.put("userId", userId);
                            jsonObject.put("foodId", foodId);
                            jsonObject.put("postDate", postDate);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String url = "http://42.48.39.55:8081/foodserver/comments/add";
                        String data = NetUtil.getData(jsonObject, url);
                        //
                        Gson gson = new Gson();
                        CommentsAddBean commentsAddBean = gson.fromJson(data, CommentsAddBean.class);
                        if (commentsAddBean.getSuccess()) {
                            Log.e(TAG, "run: " + "成功了");
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(CommentsAddActivity.this, "发表成功", Toast.LENGTH_SHORT).show();
                                }
                            });
                        } else {
                            Log.e(TAG, "run: " + "失败了");
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(CommentsAddActivity.this, "发表失败", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                }).start();

            }
        });
    }


    private void initView() {
        // 绑定控件
        mEtComments = findViewById(R.id.et_comments);
        mBtnAdd = findViewById(R.id.btn_add);
    }
}